package org.zero.onlinesubject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zero.onlinesubject.common.vo.SignRecordsVo;
import org.zero.onlinesubject.entity.SignRecords;
import org.zero.onlinesubject.entity.User;
import org.zero.onlinesubject.mapper.CourseMapper;
import org.zero.onlinesubject.mapper.SignRecordsMapper;
import org.zero.onlinesubject.service.SignRecordsService;
import org.zero.onlinesubject.utils.UserContextHolder;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class SignRecordsServiceImpl extends ServiceImpl<SignRecordsMapper, SignRecords>
    implements SignRecordsService {

    @Resource
    private SignRecordsMapper signRecordsMapper;

    @Resource
    private CourseMapper courseMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SignRecords insertSignRecord(SignRecords signRecords) {
        User user = UserContextHolder.get();
        String senderId = user.getUserid();
        signRecords.setSenderID(senderId);
        signRecords.setConfirmCount(0);
        signRecords.setPubTime(LocalDateTime.now());
        String courseID = signRecords.getCourseID();

        List<String> userIds = courseMapper.getCourseLearnUserId(courseID);
        userIds.remove(senderId);
        signRecords.setTotalCount(userIds.size());

        int rows = signRecordsMapper.insertOne(signRecords);
        if(rows==0) return null;

        Integer fromID = signRecords.getSignID();
        List<SignRecords> list = new ArrayList<>();
        for (String userId : userIds) {
            SignRecords signRecords1 = new SignRecords();
            signRecords1.setFromID(fromID);
            signRecords1.setSenderID(senderId);
            signRecords1.setRecevieID(userId);
            signRecords1.setCourseID(courseID);
            signRecords1.setLiveID(signRecords.getLiveID());
            signRecords1.setConfirmCount(0);
            signRecords1.setContent(signRecords.getContent());
            signRecords1.setPubTime(signRecords.getPubTime());
            signRecords1.setBeginTime(signRecords.getBeginTime());
            signRecords1.setEndTime(signRecords.getEndTime());
            list.add(signRecords1);
        }
        if(!list.isEmpty()){
            int rows2 = signRecordsMapper.batchInsert(list);
        }
        return signRecords;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateSignCount(Integer signID,Integer fromID) {
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        int row = signRecordsMapper.updateSignCount(now,signID,fromID);
        return row!=0;
    }

    @Override
    public SignRecordsVo getSignRecordsVo(String liveId) {
        User user = UserContextHolder.get();
        String userid = user.getUserid();
        SignRecordsVo vo = signRecordsMapper.getSignRecordsVo(userid,liveId);
        return vo;
    }

    @Override
    public List<SignRecordsVo> getSignInfoList(Integer fromId) {
        User user = UserContextHolder.get();
        List<SignRecordsVo> voList = signRecordsMapper.getSignInfoList(user.getUserid(),fromId);
        voList.forEach(vo->{
            String state = vo.getConfirmCount() != 0 ? "已签到" : "未签到";
            vo.setConfirmState(state);
        });
        return voList;
    }

    @Override
    public SignRecords getLiveSignInfo(String liveId) {
        String userId = UserContextHolder.get().getUserid();
        QueryWrapper<SignRecords> wrapper = new QueryWrapper<>();
        wrapper.eq("LiveID",liveId).isNull("FromID").eq("SenderID",userId).last(" limit 1");
        return signRecordsMapper.selectOne(wrapper);
    }
}




